﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;

namespace MongoServices
{
	/// <summary>
	/// Provides extensions for reflection methods.
	/// </summary>
	public static class ReflectionsExtensions
	{
		#region Public Static Methods
		/// <summary>
		/// Gets the attribute of the specified type.
		/// </summary>
		/// <typeparam name="T">The type of the attribute to get.</typeparam>
		/// <param name="member">The <c>MemberInfo</c> to evaluate.</param>
		/// <param name="inherit">Search the inheritence chain when finding the attribute.</param>
		/// <returns>If found, it will return an instance of the object, otherwise <c>null</c>.</returns>
		public static T GetCustomAttribute<T>(this MemberInfo member, bool inherit = false)
			where T : Attribute
		{
			var attrs = member.GetCustomAttributes(typeof(T), inherit);

			if (attrs.Length > 0)
				return (T)attrs.First();

			return null;
		}
		#endregion
	}
}
